package com.zzh.api.model;

import java.util.Collections;
import java.util.List;

/**
 * 通用返回结果
 */
public class CommonResult<T> {
    private int code;
    private String msg;
    private T data;
    private List<T> list;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public static <T> CommonResult<T> setResult(T t, int code, String msg) {
        CommonResult<T> result = new CommonResult<>();
        result.setData(t);
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    public static <T> CommonResult<T> setResultOk(T t) {
        return setResult(t, 200, "success");
    }

    public static <T> CommonResult<T> setResult(List<T> t, int code, String msg) {
        CommonResult<T> result = new CommonResult<>();
        result.setList(t);
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    public static <T> CommonResult<T> setResultOk(List<T> t) {
        CommonResult<T> result = new CommonResult<>();
        result.setList(t);
        result.setCode(200);
        result.setMsg("success");
        return setResult(t, 200, "success");
    }
}
